
public class ReverseString2 {
	
	 public static void main(String args[])
	   {
	      String str="Sample String used to test";
	      char tmp;
	      
	      int length = str.length();
	      int halfLength = length/2;
	      
	      System.out.println("Before reverse\n" + str);
	
	      for ( int i = 0; i < halfLength; i++){
	         tmp=str.charAt(i);
	         str=changeCharInPosition(i, str.charAt((length-1) - i), str);
	         str=str.replace(str.charAt(i), str.charAt((length-1) - i));
	         str=changeCharInPosition((length-1) - i, tmp, str);
	      }
	      System.out.println("After reverse\n" + str);	      
	   }
	 
	public static String changeCharInPosition(int position, char ch, String str){
		    char[] charArray = str.toCharArray();
		    charArray[position] = ch;
		    return new String(charArray);
	}

}
/* Result
Before reverse
Sample String used to test
After reverse
tset ot desu gnirtS elpmaS
*/
